// pages/changePhoneNumber/changePhoneNumber.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        last_time: '',
        is_show: true,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        this.init();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    init: function () {
        var that = this;
        wx.getStorage({
            key: 'memberInfo',
            success: function (res) {
                var data = res.data;
                var id = data.id;
                var phone_number=data.phone_number;
                that.setData({
                    id: id,
                    phone_number:phone_number
                })
            }
        })
    },//获取验证码
    clickVerify: function () {
        var that = this;
        var phone_number = that.data.phone_number;
        if (phone_number) {
            if (!app.phone_number_reg(phone_number)) {
                wx.showToast({
                    title: '号码格式错误',
                    image: '../../images/warning.png'
                })
                return;
            }
        }
        var that = this;
        that.setData({
            is_show: false   //false
        })
        settime(that);
        that.request_get_code(phone_number)
    },
    get_phone_number: function (e) {
        var that = this;
        var phone_number = e.detail.value;
        that.setData({
            phone_number: phone_number
        })
    },
    get_code: function (e) {
        var that = this;
        var code = e.detail.value;
        that.setData({
            code: code
        })
    },
    request_get_code: function (phone_number) {
        var that = this;
        var params = {};
        params['phone_number'] = phone_number;
        params['code'] = 2;
        wx.request({
            url: app.config() + "member/xiaoSendCode",
            data: params,
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {

            }
        })
    },
    submit: function () {
        var that = this;
        var phone_number = that.data.phone_number;
        var verify_code = that.data.code;
        var id = that.data.id;
        var params = {};
        params['id'] = id;
        params['phone_number'] = phone_number;
        params['verify_code'] = verify_code;
        wx.request({
            url: app.config() + "member/xiaoUpdateMemberInfo",
            data: params,
            header:{
                'content-type':'application/json'
            },
            success:function(res){
                if(res.data.data.status==0){
                    wx.showToast({
                        title: '更新成功',
                        icon:"success"
                    })
                    wx.switchTab({
                        url: '../../pages/myPage/myPage'
                    })
                }else{
                    wx.showToast({
                        title: '更新失败',
                        image: "../../images/warning.png"
                    })
                }
            }

        })
    }
})
var countdown = 60;
var settime = function (that) {
    if (countdown == 0) {
        that.setData({
            is_show: true,
            last_time: ""
        })
        countdown = 60;
        return;
    } else {
        that.setData({
            is_show: false,
            last_time: countdown
        })
        countdown--;
    }
    setTimeout(function () {
        settime(that)
    }, 1000)
}